/**
 * @(#)ThirdSmsConfigController.java 2017年08月02日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import org.jsbd.boss.annotation.SystemControllerLog;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.channel.ThirdSmsConfig;
import org.jsbd.boss.service.IInterceptInfoService;
import org.jsbd.boss.service.IThirdSmsConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 第三方获取短信以及验证码
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/smsconfig")
public class ThirdSmsConfigController {
    public static final Logger LOGGER = LoggerFactory.getLogger(ThirdSmsConfigController.class);
    @Resource
    private IThirdSmsConfigService thirdSmsConfigService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list(Model model) {
        return "smsconfig/list";
    }

    @RequestMapping(value = "configList")
    @ResponseBody
    public Map<String, Object> configPageList(ThirdSmsConfig thirdSmsConfig) {
        Map<String, Object> map = new HashMap<String, Object>();
        Integer isUseful = thirdSmsConfig.getIsUseful();
        if (isUseful != null && isUseful == -1) {
            thirdSmsConfig.setIsUseful(null);
        }
        List<ThirdSmsConfig> thirdSmsConfigs = thirdSmsConfigService.selectPageList(thirdSmsConfig);
        Integer count = thirdSmsConfigService.selectCount(thirdSmsConfig);
        map.put("rows", thirdSmsConfigs);
        map.put("total", count);
        return map;
    }

    @RequestMapping(value = "{id}/update", method = RequestMethod.GET)
    public String update(@PathVariable Integer id, Model model) {
        if (id != null) {
            ThirdSmsConfig config = thirdSmsConfigService.selectConfig(id);
            model.addAttribute("smsconfig", config);
        }
        return "smsconfig/update";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public void update(HttpServletResponse response, ThirdSmsConfig thirdSmsConfig) {
        try {
            thirdSmsConfigService.updateThirdSmsConfig(thirdSmsConfig);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "{id}/effect")
    @SystemControllerLog(description = "生效")
    public void effect(@PathVariable Integer id, HttpServletResponse response) {
        try {
            if (id != null) {
                ThirdSmsConfig config = thirdSmsConfigService.selectConfig(id);
                config.setIsUseful(1);
                thirdSmsConfigService.updateThirdSmsConfig(config);
                ResponseUtils.responseSuccess(response);
            }
        } catch (Exception e) {
            LOGGER.error("生效apk出错", e);
        }
        ResponseUtils.responseFailure(response);
    }

    @RequestMapping(value = "{id}/abandon")
    @SystemControllerLog(description = "生效")
    public void abandon(@PathVariable Integer id, HttpServletResponse response) {
        try {
            if (id != null) {
                ThirdSmsConfig config = thirdSmsConfigService.selectConfig(id);
                config.setIsUseful(0);
                thirdSmsConfigService.updateThirdSmsConfig(config);
                ResponseUtils.responseSuccess(response);
            }
        } catch (Exception e) {
            LOGGER.error("生效apk出错", e);
        }
        ResponseUtils.responseFailure(response);
    }

    @RequestMapping(value = "{id}/delete")
    public void del(HttpServletResponse response, @PathVariable Integer id) {
        try {
            thirdSmsConfigService.delThirdSmsConfig(id);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add() {
        return "smsconfig/add";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public void add(HttpServletResponse response, ThirdSmsConfig thirdSmsConfig) {
        try {
            thirdSmsConfig.setIsUseful(0);
            Integer smsId = thirdSmsConfig.getSmsId();
            if(smsId == null){
                thirdSmsConfig.setSmsId(-1);
            }
            thirdSmsConfigService.insertThirdSmsConfig(thirdSmsConfig);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            ResponseUtils.responseFailure(response);
        }
    }

}
